<?php
abstract class tag{
        public static $NEW_LINE="\n";
        protected $text="";
	protected $tag="";
	protected $children;
	protected $tab="   ";
	protected $tabNumber=0;
	protected $attributes;
	protected $mustClose=TRUE;
        protected $textBefore=TRUE;
        protected $singleLine=FALSE;
        protected $allowSingleTag=FALSE;
	
	function __construct() {
		$this->children=array();
		$this->attributes=array();
	}
	
	function __toString(){
		$result="";
                $nl=$this->singleLine?"" : tag::$NEW_LINE;
		$result=$result.$this->getTab()."<".$this->getTag();
		if (sizeof($this->attributes)>0){
			foreach ($this->attributes as $name => $value){
				$result=$result." ".$name."=\"".$value."\"";
			}
		}
                if ($this->allowSingleTag && $this->mustClose && strlen($this->getText())==0 && sizeof($this->children)==0)
                    return $result."/>".$nl;
		$result=$result.">".$nl;
		
		if (strlen($this->getText())>0 && $this->textBefore){
                    if (!$this->singleLine)
			$result=$result.$this->getTab().$this->tab.$this->getText().$nl;
                    else
                        $result=$result.$this->getText();
                }

		for ($i=0;$i<sizeof($this->children);$i++){
			$result=$result.$this->children[$i];
		}

                if (strlen($this->getText())>0 && !$this->textBefore){
                    if (!$this->singleLine)
                        $result=$result.$this->getTab().$this->tab.$this->getText().$nl;
                    else
                        $result=$result.$this->getText();
                }

		if ($this->mustClose){
                    if (!$this->singleLine)
			$result=$result.$this->getTab()."</".$this->getTag().">".tag::$NEW_LINE;
                    else
                        $result=$result."</".$this->getTag().">".tag::$NEW_LINE;
                }
		return $result;
	}
	
	public function getText(){
		return utf8_encode($this->text);
	}
	
	public function setText($txt){
		$this->text=$txt;
	}

	public function getTag(){
		return $this->tag;
	}
	
	public function setTag($tag){
		$this->tag=$tag;
	}
	
	public function addChild($element){
		$element->addTab($this->tabNumber+1);
		$this->children[sizeof($this->children)]=$element;
		$this->setTabNumber($this->tabNumber);
	}
	
	public function setTabNumber($tabN){
		if (sizeof($this->children)>0){
			foreach ($this->children as $elm){
				$elm->setTabNumber($tabN+1);	
			}
		}
		$this->tabNumber=$tabN;
	}
	
	public function addTab($added){
		$this->tabNumber=$this->tabNumber+$added;
	}
	
	public function addAttribute($name,$value){
		$this->attributes[$name]=$value;
	}
	public function getTab(){
		$calc_tab="";
		for($i=0;$i<$this->tabNumber;$i++){
			$calc_tab=$calc_tab.$this->tab;
		}
		return $calc_tab;
	}

        public function isTextBefore(){
            return $this->textBefore;
        }

        public function setTextBefore($textBefore){
            $this->textBefore=$textBefore;
        }

        public function isSingleLine(){
            return $this->singleLine;
        }

        public function setSingleLine($singleLine){
            $this->singleLine=$singleLine;
        }

        public function isAllowSingleTag(){
            return $this->allowSingleTag;
        }

        public function setAllowSingleTag($allowSingleTag){
            $this->allowSingleTag=$allowSingleTag;
        }
}
?>